//
//  NKCaseAddOrmodifyVC.m
//  NewKY
//
//  Created by MXQ on 2016/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKCaseAddOrmodifyVC.h"
#import <HMImagePickerController.h>
#import "NKCloudViewController.h"
#import "NKCaseChoseVC.h"
#import "NKCaseSelectTableViewCell.h"
#import "NKLableLayout.h"
#import "NKCloudCollectionViewCell.h"
#import "NKCasePictureVC.h"
#import "NKPictureCollectionViewCell.h"
#import "UILabel+CZAddition.h"
#import "UIButton+CZAddition.h"
#import "NKCaseInform.h"
#define MAX_LIMIT_NUMS 20


@interface NKCaseAddOrmodifyVC ()<UITableViewDelegate,UITableViewDataSource,HMImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,NKLabelLayoutDelegate,UITextViewDelegate>

//记录主界面传来的病例List
//@property(nonatomic,strong)NSMutableArray <NKCaseInform*>* caseInforList;

/*---- text -----*/
@property(nonatomic,strong)UITextView *textView;

/*---- 占位label -----*/
@property(nonatomic,strong)UILabel *placeholdeLabel;


@property(nonatomic,strong) NSMutableArray * deletBUttons;


/*---- 显示剩余输入 -----*/
@property(nonatomic,strong)UILabel *wordCountLabel;

//记录标签数组
@property (nonatomic, strong) NSMutableArray* titles;

//云标签View
@property (nonatomic, weak) UICollectionView* collectionView;

@property (nonatomic) NSArray *images;
/// 选中资源素材数组，用于定位已经选择的照片
@property (nonatomic) NSArray *selectedAssets;

//记录疾病选择的Table
@property(nonatomic,weak)UITableView * tableView;

//记录是哪一个疾病
@property(nonatomic,assign) NSInteger illtypeNum;

//记录展示图片的View
@property(nonatomic,weak) UICollectionView * pictureDispalyView;

//临时记录

@property(nonatomic,weak)UIImageView * tempView;
@property(nonatomic,weak)NKPictureCollectionViewCell * lastCell;


@end

@implementation NKCaseAddOrmodifyVC
static NSString* identifier = @"cell";


- (NSArray *)titles
{
    if (!_titles) {
        _titles = [NSMutableArray arrayWithArray:@[]];
        
    }
    return _titles;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.deletBUttons = [[NSMutableArray alloc]init];
    
//    self.caseInforList = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [self.collectionView reloadData];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passAction:) name:@"passValue" object:nil];

    
    [self setupCloudDisplayView];
    
    [self setupMainUI];
    
    [self setupTextView];
    


    
}

#pragma mark - 相册多选展示
- (void)imagePickerController:(HMImagePickerController *)picker
      didFinishSelectedImages:(NSArray<UIImage *> *)images
               selectedAssets:(NSArray<PHAsset *> *)selectedAssets {
    
    // 记录图像，方便在 CollectionView 显示
    self.images = images;
    // 记录选中资源集合，方便再次选择照片定位
    self.selectedAssets = selectedAssets;
    
    [self.pictureDispalyView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)setupPictureView{
    
    UICollectionViewFlowLayout * fl = [[UICollectionViewFlowLayout alloc]init];


    
    fl.itemSize = CGSizeMake(90, 90);
    
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(50, 50, 200, 200) collectionViewLayout:fl];
    
    
    [collectionView registerClass:[NKPictureCollectionViewCell class] forCellWithReuseIdentifier:@"KAKA"];
    
    [self.view addSubview:collectionView];
    
    collectionView.backgroundColor = [UIColor greenColor];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    collectionView.contentInset = UIEdgeInsetsMake(10, 0,0 , 0);
    
    self.pictureDispalyView = collectionView;
    
}


- (void)btnAction:(UIButton*)button {
    
    
    
        NSInteger  integer = button.tag;
    
        NSMutableArray * arr = [[NSMutableArray alloc]initWithArray:self.images];
    
    
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
            if (idx == integer) {
    
                *stop = YES;
    
            };
    
            if (*stop == YES) {
    
                [arr removeObject:obj];
    
    
    
            }
    
    
        }];
    self.deletBUttons = nil;
    
    [button removeFromSuperview];
        
        self.images = arr.copy;
        
        [self.pictureDispalyView reloadData];
    
}

-(void)viewWillAppear:(BOOL)animated{

    [self.deletBUttons removeLastObject];

}
-(void)viewDidAppear:(BOOL)animated{

    [self.deletBUttons removeLastObject];





}


#pragma mark - 创建展示云标签的Collection
-(void)setupCloudDisplayView{
    
    //创建CollectionView LayOut
    NKLableLayout* layout = [[NKLableLayout alloc] init];
    layout.panding = 10;
    layout.rowPanding = 20;
    layout.delegate = self;
    
    //创建CollectionView
    UICollectionView* collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerNib:[UINib nibWithNibName:@"NKCloudCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];
    [self.view addSubview:collectionView];
   
}
//添加云标签

-(void)addCludeClick:(UIButton*)btn{
    
    
    NKCloudViewController * aludeVc = [[NKCloudViewController alloc]init];
    
    aludeVc.couldSaveList = self.titles;
    
    

    
    aludeVc.illCloudDidSelectBlock =^(NSArray * cloudList){
        
        NSLog(@"%@",cloudList);
    [self.collectionView reloadData];
    
        
        [self.titles arrayByAddingObjectsFromArray:cloudList];
       
        
        NSLog(@"self.titles = %@",self.titles);

        
        for (NSString * item in cloudList) {
            
            NSLog(@"self.titles = %@",self.titles);
            
            
            
            [self.titles addObject:item];
//            [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:_titles.count - 1 inSection:0]]];
//            
            
        }
        
    };
    
    
    [self.navigationController pushViewController:aludeVc animated:YES];
    
    
}


// OJLLabelLayoutDelegate
-(NSArray *)NKLabelLayoutTitlesForLabel{
    return self.titles;
}



#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (collectionView == self.collectionView) {
    return 1;
        
        
    }else{
    
        return 1;
    
    }
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (collectionView == self.collectionView) {
        
        
        return self.titles.count;
        
    }else{
    return self.images.count+1;
    
    }
    
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   

    if (collectionView == self.collectionView) {
        
        
        
        NKCloudCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        
        [cell setTitle:self.titles[indexPath.item]];
        
        return cell;
        
    }else{
    
        NKPictureCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KAKA" forIndexPath:indexPath];

        [cell.contentView removeAllSubviews];
        
//        imgv
        UIImageView * imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_pic_add_highlighted"]];
        
        [cell.contentView addSubview:imgV];
        
        [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.equalTo(cell.contentView).offset(10);
            make.bottom.right.equalTo(cell.contentView).offset(-10);
        }];
        
        
        
        
        //btn
        UIButton * btn = [[UIButton alloc]init];
        
        btn.tag = indexPath.row;
        
        btn.size = CGSizeMake(20, 20);
        
        [btn setImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        
        [btn addTarget:self action:@selector(btnActionDelete:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.contentView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //        make.right.equalTo(self.contentView).offset(-8);
            //        make.top.equalTo(self.contentView).offset(8);
            
                    make.centerX.equalTo(imgV.mas_right);
                    make.centerY.equalTo(imgV.mas_top);
                }];
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
//        NKPictureCollectionViewCell * cell = [[NKPictureCollectionViewCell alloc]init];
        
//        
//        UIButton * btn = [[UIButton alloc]init];
//        
//        
//        cell.deletButton = btn;
//        
//        [cell.deletButton addTarget:self action:@selector(btnActionDelete:) forControlEvents:UIControlEventTouchUpInside];
//        cell.deletButton.tag = indexPath.row;
//        
//        
//        
//        
        if (indexPath.row <= self.images.count-1 && self.images.count != 0) {
            
            imgV.image =  self.images[indexPath.row];
            
        }
        if (indexPath.row == self.images.count) {
            
          imgV.image = [UIImage imageNamed:@"compose_pic_add_highlighted"];
            
            if (btn.tag >=0 ) {
                
                [cell.contentView removeAllSubviews];
                
                //        imgv
                UIImageView * imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_pic_add_highlighted"]];
                
                [cell.contentView addSubview:imgV];
                
                [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.left.equalTo(cell.contentView).offset(10);
                    make.bottom.right.equalTo(cell.contentView).offset(-10);
                }];
                
            }
        }
//
//        NSLog(@"%@",cell);
  
        
        return cell;
    
    
    
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        
      NSLog(@"%@",self.titles[indexPath.item]);  
        
    }else{
    
    
        HMImagePickerController *picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];
        
        // 设置图像选择代理
        picker.pickerDelegate = self;
        // 设置目标图片尺寸
        picker.targetSize = CGSizeMake(600, 600);
        // 设置最大选择照片数量
        picker.maxPickerCount = 9;
        
        [self presentViewController:picker animated:YES completion:nil];
        
        
    }
}








#pragma mark - 疾病选择和细分的TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViewP{
    
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (NKCaseSelectTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NKCaseSelectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"KAKa" forIndexPath:indexPath];
    
    cell.illLable.text = indexPath.row == 0 ? @"疾病类型" : @"疾病细分";
    cell.choiceLable.text = @"选择";
    return cell;
    
}

//tableview代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NKCaseChoseVC * vc = [[NKCaseChoseVC alloc]init];
    
    //识别VC
    vc.isTypeVC = indexPath.row == 0 ? YES : NO;
    
    //识别疾病
    vc.illtypeNum = self.illtypeNum;
    
    vc.illTypeDidSelectBlock = ^(NSString* illType,NSInteger illtypeNum){
    
        NKCaseSelectTableViewCell * cell =  [self.tableView cellForRowAtIndexPath:indexPath];
        
        cell.choiceLable.text = illType;
    
        self.illtypeNum = illtypeNum;
        
        //疾病类型选择回来 将疾病细分的内容设置为“选择”
//        if (vc.isTypeVC == YES)
//        {
//       
//            NKCaseSelectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"KAKa" forIndexPath:indexPath];
//       
//            cell.choiceLable.text = @"选择";
//            
//        };
        

        
    
    } ;
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}


#pragma mark - =========== 搭建主UI ==============
- (void)setupMainUI{
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    [tableView registerClass:[NKCaseSelectTableViewCell class] forCellReuseIdentifier:@"KAKa"] ;
    
    [self.view addSubview:tableView];
    
    
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(12);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_top).offset(98);
        
    }];
    
    
    
    
    tableView.dataSource =self;
    tableView.delegate = self;
    
    self.tableView = tableView;
    
    //疾病描述Lable
    
//    UILabel * illDiscrLb = [[UILabel alloc]init];
//    illDiscrLb.text = @"疾病描述";
//    
//    [self.view addSubview:illDiscrLb];
//    
//    
//    [illDiscrLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.equalTo(tableView.mas_bottom).offset(20);
//        make.left.equalTo(self.view).offset(20);
//        
//        
//    }];
    

    //话筒
    UIView * voiceView = [[UIView alloc]init];
    
    voiceView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:voiceView];
    
    [voiceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tableView.mas_bottom).offset(16);
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(@48);
        make.height.equalTo(@90);
        
    }];
    
    //voice
    UIButton*  voice = [[UIButton alloc]init];

    [voiceView addSubview:voice];
    
    [voice setImage:[UIImage imageNamed:@"ic_assessment_2x"] forState:UIControlStateNormal];
    
    
    
    [voice mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(voiceView).offset(8);
        make.left.equalTo(voiceView);
        make.height.equalTo(@37);
        make.right.equalTo(voiceView).offset(-7);
    }];

        [voice addTarget:self action:@selector(addCludeClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    //    //云标签展示的collectionView
    //
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(tableView.mas_bottom).offset(16);
        make.left.equalTo(voiceView.mas_right);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@70);
        
    }];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];

    //云标签展示的Lable
    UILabel * cloudeAddLabe= [UILabel cz_labelWithText:@"添加几个标签，描述您的症状  " fontSize:13
                                                 color:[UIColor grayColor]];
    
    cloudeAddLabe.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:cloudeAddLabe];
    
    [cloudeAddLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view);
        make.top.equalTo(self.collectionView.mas_bottom);
        
    }];
    
    //填补的白色View
    UIView * viewNouse = [[UIView alloc]init];
    
    viewNouse.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewNouse];
    
    [viewNouse mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        make.right.equalTo(cloudeAddLabe.mas_left);
        
        make.top.bottom.equalTo(cloudeAddLabe);
        
    }];
  
    
    //textInputView
    UITextView * textView = [[UITextView alloc]init];
    
    textView.backgroundColor = [UIColor whiteColor];
    
    self.textView = textView;
    [self.view addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(viewNouse.mas_bottom);
        
        make.left.equalTo(self.view).offset(68);
        make.right.equalTo(self.view);
        
        make.height.equalTo(@100)
        ;
        
        
    }];
    
    
    

    //疾病描述Lable
    
//    UILabel * illPic = [[UILabel alloc]init];
//    
//    illPic.text = @"疾病图片:";
//    
//    [self.view addSubview:illPic];
//    
//    
//    [illPic mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.equalTo(textView.mas_bottom).offset(20);
//        make.left.equalTo(self.view).offset(20);
//        
//        
//    }];
    
    
    //已选照片展示的View
    
    [self setupPictureView];

 
    [self.view addSubview:self.pictureDispalyView];
    
    [self.pictureDispalyView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(textView.mas_bottom);
        
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@190);
        
    }];
    
    
    
    self.pictureDispalyView.backgroundColor = [UIColor whiteColor];
    
    

    //lastLable
    UILabel * lableLast = [UILabel cz_labelWithText:@"想要我们更加了解病情？多传几张照片试试" fontSize:13.5 color:
                           [UIColor orangeColor]];
    lableLast.backgroundColor = [UIColor whiteColor];
    lableLast.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:lableLast];
    
    [lableLast mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_pictureDispalyView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
        
    }];
    
    //lastWhiteView
    UIView * lastWhiteView = [[UIView alloc]init];

    lastWhiteView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:lastWhiteView];
    
    [lastWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(lableLast.mas_bottom).offset(0);
        make.height.equalTo(@2);
        
        
        
    }];
    
    
    
    
    
    
    //保存按钮
    UIButton * saveButton = [UIButton cz_textButton:@"          一键保存到病例，新K医更关心你                     " fontSize:16 normalColor:
                             [UIColor blackColor] selectedColor:[UIColor blackColor]];
    
    saveButton.backgroundColor = [UIColor whiteColor];
    

    [saveButton setImage:[UIImage imageNamed:@"ic_assignment_turned_in_2x"] forState:UIControlStateNormal];

    [saveButton addTarget:self action:@selector(saveToCache) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:saveButton];
    
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(lastWhiteView.mas_bottom).offset(10);
        make.height.equalTo(@43);
        
    }];
    
    UIView * astWhiteView = [[UIView alloc]init];
    
    astWhiteView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:astWhiteView];
    
    [astWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(saveButton.mas_bottom).offset(8);
        make.height.equalTo(@90);
        
        
        
    }];

    
    
    //loneLong
    
    UIView * lineLong1 = [[UIView alloc]init];
    
    lineLong1.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [self.view addSubview:lineLong1];
    
    [lineLong1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view);
        make.top.equalTo(textView.mas_bottom).offset(-0.1);
        make.left.equalTo(self.view).offset(8);
        make.height.equalTo(@2);
        
    }];
    

    //分割线
    UIView * lineView = [[UIView alloc]init];
    
    [self.view addSubview:lineView];
    
    lineView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(54.5);
        make.left.equalTo(self.view).offset(9);
        make.right.equalTo(self.view);
        make.height.equalTo(@2);
    }];

    
    //textView的填补View
    
    UIView * textViewAddView = [[UIView alloc]init];
    
    textViewAddView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:textViewAddView];
    
    [textViewAddView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(viewNouse.mas_bottom);

        make.left.equalTo(self.view);
        make.height.equalTo(@100);
        make.width.equalTo(@68);
        
    }];
    
    // textInputBtn
    UIButton * textInoutBtn = [[UIButton alloc]init];
    
    [textInoutBtn setImage:[UIImage imageNamed:@"ic_description_2x"] forState:UIControlStateNormal];
    [textViewAddView addSubview:textInoutBtn];
    
    [textInoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(textViewAddView);
        make.right.equalTo(textViewAddView.mas_right).offset(-27);
        make.top.equalTo(textViewAddView).offset(25);
        make.width.equalTo(@27);
    }];

    //竖线2
    UIView * line7 = [[UIView alloc]init];
    
    [textViewAddView addSubview:line7];
    
    line7.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [line7 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(textViewAddView).offset(30);
        make.right.equalTo(textViewAddView).offset(-20);
        make.width.equalTo(@1.8);
        make.height.equalTo(@21);
        
    }];
    
    //又一条横线
    
    //竖线1
    UIView * line1 = [[UIView alloc]init];
    
    [voiceView addSubview:line1];
    
    line1.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(voiceView).offset(13);
        make.right.equalTo(voiceView);
        make.width.equalTo(@1.8);
        make.height.equalTo(@21);
        
    }];

    //loneLong 740463782
    
    UIView * lineLong = [[UIView alloc]init];
    
    lineLong.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
    
    [self.view addSubview:lineLong];
    
    [lineLong mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view);
        make.top.equalTo(viewNouse.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(8);
        make.height.equalTo(@2);
        
    }];
    
    
}

#pragma mark - 持久化病例

-(void)saveToCache{

    
    
    [self saveHero];
    
//    [self loadPerson];
    
    
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)saveHero
{
    
    //获取疾病的名字
    NKCaseSelectTableViewCell * caseChoseCell =  [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    if ([caseChoseCell.choiceLable.text isEqualToString:@"选择"]) {
        NSLog(@"弹窗： 请选则疾病类型");
    }
    
    //获取详细描述
//     self.textView.text ;

    //获取当前时间
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSLog(@"dateString:%@",dateString);
    
    
    NKCaseInform *hero = [[NKCaseInform alloc] init];
    hero.caseName = caseChoseCell.choiceLable.text;
    hero.illImage = self.images.firstObject;
    hero.caseDiscreb = self.textView.text ;
    hero.saveDate = dateString;
    
    // -------- 获取保存路径 --------
    NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //    NSString *path = [docDir stringByAppendingPathComponent:@"Hero.plist"];
    NSString *path = [docDir stringByAppendingPathComponent:@"Hero.hm"];
    

    
    
    // -------- 使用数组来保存对象 --------
     [self.caseInforList insertObject:hero atIndex:0];//    [NSKeyedArchiver archiveRootObject:tempArr toFile:path];
//    [tempArr writeToFile:path atomically:YES];
    [NSKeyedArchiver archiveRootObject:self.caseInforList toFile:path];

//
    /**
     *  数组|字典 不能保存对象类型(没有错误提示), 只能是NSString|NSNumber|NSDate|NSData类型
     */
    
    
    // -------- 使用归档来保存自定义对象 --------
    
    // NSKeyedArchiver 提供了归档相关操作的方法
    
    /**
     *  将支持NSCoding协议的对象进行归档保存
     * 要保存的对象, 必须遵守NSCoding协议
     
     * 该方法会调用对象的 encodeWithCoder 方法进行键值映射
     */
//    [NSKeyedArchiver archiveRootObject:hero toFile:path];
}



#pragma mark - textView setUp
-(void)setupTextView{

    /*---- 设置label -----*/
    _placeholdeLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 3, [UIScreen mainScreen].bounds.size.height, 50)];
    _placeholdeLabel.font = [UIFont systemFontOfSize:15];
    _placeholdeLabel.textColor = [UIColor lightGrayColor];
    _placeholdeLabel.text = @"";
    _placeholdeLabel.numberOfLines = 0;
    _placeholdeLabel.enabled = NO;
    [self.textView addSubview:_placeholdeLabel];
    
    /*---- 右下角显示剩余输入label -----*/
    UILabel *wordCountLabel = [[UILabel alloc]init];
    
    wordCountLabel.font = [UIFont systemFontOfSize:13];
    wordCountLabel.textColor = [UIColor lightGrayColor];
    wordCountLabel.enabled = NO;
    wordCountLabel.text = @"还可以输入20位";
        wordCountLabel.backgroundColor = [UIColor redColor];
    [self.textView addSubview:wordCountLabel];
    self.wordCountLabel = wordCountLabel;
    
    [wordCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.bottom.equalTo(self.textView).offset(-8);
        
    }];
    
    
    
    
    
    /*---- 弹出键盘 -----*/
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
    
    [topView setItems:buttonsArray];
    [self.textView setInputAccessoryView:topView];
    





}


- (void)textViewDidChange:(UITextView *)textView{
    
    if (self.textView.text.length != 0) {
        
        _placeholdeLabel.hidden = YES;
        
    }else{
        //为了保证在删除所输入的内容的情况下，提示符再次出现
        _placeholdeLabel.hidden = NO;
    }
    
    //    NSString *nsTextContent = textView.text;
    //    NSInteger existTextNum = nsTextContent.length;
    //    if (existTextNum > MAX_LIMIT_NUMS) {
    //
    //        NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
    //        [textView setText:s];
    //    }
    
    
    
    /*---- 添加右下角的输入限制 -----*/
    
    unsigned long count;
    count = MAX_LIMIT_NUMS - textView.text.length;
    
    [self.wordCountLabel setText:[NSString stringWithFormat:@"还可以输入%lu位",count]];
    
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    NSInteger caninputlen = MAX_LIMIT_NUMS - comcatstr.length;
    if (caninputlen >= 0 ) {
        return  YES;
    }
    else
    {
        NSInteger len = text.length + caninputlen;
        NSRange rg = {0 ,MAX(len,0)};
        
        if (rg.length > 0) {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
    
    
}


//关闭键盘
-(void) dismissKeyBoard{
    
    [self.textView resignFirstResponder];
}






//删除照片

-(void)btnActionDelete:(UIButton*)button{

    NSInteger  integer = button.tag;
    
        NSMutableArray * arr = [[NSMutableArray alloc]initWithArray:self.images];
    
    
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
            if (idx == integer) {
    
                *stop = YES;
    
            };
    
            if (*stop == YES) {
    
                [arr removeObject:obj];
    
    
    
            }
    
    
        }];
    
        
        self.images = arr.copy;
        
        [self.pictureDispalyView reloadData];




}


//
//-(void)passAction:(NSNotification *)noti{
//    
//    NSInteger  integer = ((UIButton*)(noti.object)).tag;
//
//    NSMutableArray * arr = [[NSMutableArray alloc]initWithArray:self.images];
//    
//    
//    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        
//        if (idx == integer) {
//            
//            *stop = YES;
//            
//        };
//        
//        if (*stop == YES) {
//            
//            [arr removeObject:obj];
//            
//            
//            
//        }
//        
//        
//    }];
//    
//    
//    self.images = arr.copy;
//    
//    [self.pictureDispalyView reloadData];
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
